package com.parleys.android.client.service;

import android.content.Context;
import android.content.Intent;
import android.os.ResultReceiver;
import android.widget.ImageView;

import com.parleys.android.client.util.ImageDownloader;

public class ParleysServiceHelper {

	private static ParleysServiceHelper _instance = null;
	private final ImageDownloader imageDownloader = new ImageDownloader();
	
	private ParleysServiceHelper() {
	}
	
	private synchronized static void createInstance() {
		if (_instance == null) {
			_instance = new ParleysServiceHelper();
		}
	}
	
	public static ParleysServiceHelper getInstance() {
		if (_instance == null) {
			createInstance();
		}
		return _instance;
	}
	
	public void getGeneralNews(Context context, ResultReceiver receiver) {
		final Intent intent = new Intent(Intent.ACTION_SYNC, null, context, ParleysService.class);
        intent.putExtra(ParleysService.EXTRA_STATUS_RECEIVER, receiver);
        intent.putExtra(ParleysService.EXTRA_REST_OPERATION, ParleysService.GENERAL_NEWS_REST_OPERATION);
		context.startService(intent);
	}
	
	public void getSpaces(Context context, ResultReceiver receiver) {
		final Intent intent = new Intent(Intent.ACTION_SYNC, null, context, ParleysService.class);
        intent.putExtra(ParleysService.EXTRA_STATUS_RECEIVER, receiver);
        intent.putExtra(ParleysService.EXTRA_REST_OPERATION, ParleysService.SPACES_REST_OPERATION);
		context.startService(intent);
	}
	
	public void getChannels(Context context, int spaceId, ResultReceiver receiver) {
		final Intent intent = new Intent(Intent.ACTION_SYNC, null, context, ParleysService.class);
        intent.putExtra(ParleysService.EXTRA_STATUS_RECEIVER, receiver);
        intent.putExtra(ParleysService.EXTRA_REST_OPERATION, ParleysService.CHANNELS_REST_OPERATION);
        intent.putExtra(ParleysService.EXTRA_SPACE_ID, spaceId);
		context.startService(intent);
	}
	
	public void getPresentations(Context context, int channelId, ResultReceiver receiver) {
		final Intent intent = new Intent(Intent.ACTION_SYNC, null, context, ParleysService.class);
        intent.putExtra(ParleysService.EXTRA_STATUS_RECEIVER, receiver);
        intent.putExtra(ParleysService.EXTRA_REST_OPERATION, ParleysService.PRESENTATIONS_REST_OPERATION);
        intent.putExtra(ParleysService.EXTRA_CHANNEL_ID, channelId);
		context.startService(intent);
	}
	
	public void downloadImage(String url, ImageView view) {
		imageDownloader.download(url, view);
	}
	
}
